/**
 * 
 */
package optimization.naturalOptimization.fitness;

import multithreading.FitnessCalculator;
import optimization.naturalOptimization.population.Population;

/**
 * Test fitness function to test algorithms. f(x)=sum<sub>i=1</sub><sup>n</sup>
 * [x<sub>i</sub><sup>2</sup>] This function has a minimum at value 0 in every
 * dimension. The fitness value in optimum is 0.
 * 
 * @see <a
 *      href="http://www-optima.amp.i.kyoto-u.ac.jp/member/student/hedar/Hedar_files/TestGO_files/Page1113.htm">Sphere
 *      Function</a>
 * @author Kevin Wagner
 * @version 1.0
 */
public class Sphere extends SimpleFitnessFunction<double[]> {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * optimization.naturalOptimization.fitness.SimpleFitnessFunction#getFitness
	 * (java.lang.Object)
	 */
	@Override
	public double getFitness(double[] position) {
		double erg = 0.0;
		for (int i = 0; i < position.length; i++) {
			erg += position[i] * position[i];
		}
		return erg;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see optimization.IdentificationI#getIndentification()
	 */
	@Override
	public String getIndentification() {
		return "Test Function: Sphere";
	}

	@Override
	public void getFitness(Population pop) throws Exception{
		int ind=pop.getNumberOfIndividuals();
		FitnessCalculator[] calculators=new FitnessCalculator[ind];
		for(int i=0;i<ind;i++){
			calculators[i]=new FitnessCalculator(pop.getIndividual(i), this);
			calculators[i].start();
		}
		for(int i=0;i<ind;i++){
				calculators[i].join();
		}
	}

}
